package com.cloudsigns.factorymanager2.engineering.utils;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by tony on 2018/4/14.
 */

public class ImageManager {

    private static ImageManager instance;

    public static ImageManager getInstance() {
        synchronized (ImageManager.class) {
            if (null == instance) {
                instance = new ImageManager();
            }
        }
        return instance;
    }

    public void uploadImage(String filePath, final IUploadCallBack iUploadCallBack) {
        OkHttpClient mOkHttpClent = new OkHttpClient();
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        File file = new File(filePath);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", fileName,
                        RequestBody.create(MediaType.parse("image/jpg"), file));

        final RequestBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url("http://120.78.214.229:10001/uploadFile")
                .post(requestBody)
                .build();
        Call call = mOkHttpClent.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(ImageManager.class.getName(), "onFailure: " + e);
                if (iUploadCallBack != null) {
                    iUploadCallBack.onFailure(e.toString());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
               /* Log.e(ImageManager.class.getName(), fileName+" "+response.toString()
                +" body=" +response.body().string());*/
//{"code":0,"host":"http://120.78.214.229:10000","url":"/2017/11/29/4d2099b5-3ada-4524-8878-0be54108efdd.jpg"}
                if (iUploadCallBack != null) {
                    JSONObject jsonObject = null;
                    String url = null;
                    String error = null;
                    try {
                        String body = response.body().string();
                        if (!TextUtils.isEmpty(body) && body.startsWith("\ufeff")) {
                            body = body.substring(1);
                        }
                        jsonObject = new JSONObject(body);
                        url = jsonObject.getString("host") + jsonObject.getString("url");

                    } catch (JSONException e) {
                        e.printStackTrace();
                        error = e.toString();
                    } catch (Exception e) {
                        e.printStackTrace();
                        error = e.toString();
                    }
                    if (!TextUtils.isEmpty(url)) {
                        iUploadCallBack.onResponse(url);
                    } else {
                        iUploadCallBack.onFailure(error);
                    }

                }
            }
        });

    }


    public interface IUploadCallBack {
        void onFailure(String fail);


        void onResponse(String url) throws IOException;
    }
}
